package com.ruoyi.cms.controller;

import com.ruoyi.cms.domain.CmsDict;
import com.ruoyi.cms.service.ICmsDictService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "CmsLanguageController", description = "系统支持的语言管理 for管理后台")
@RestController
@RequestMapping("cms/manager/language")
public class CmsLanguageController extends BaseController {


    @Autowired
    private ICmsDictService cmsDictService;


    @Operation(summary = "查询所有支持的语言列表")
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        CmsDict cmsDict = new CmsDict();
        cmsDict.setDictType("language");
        List<CmsDict> list = cmsDictService.selectCmsDictList(cmsDict);
        return getDataTable(list);
    }

    @Operation(summary = "查询详情")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        CmsDict dict = cmsDictService.selectCmsDictById(id);
        return success(dict);
    }

    /**
     * 新增分类
     */
    @Log(title = "新增网站支持语言", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsDict cmsDict) {
        cmsDict.setDictType("language");
        cmsDict.setCode("language");
        cmsDict.setSort(0L);
        return toAjax(cmsDictService.insertCmsDict(cmsDict));
    }

    /**
     * 修改分类
     */
    @Log(title = "编辑网站支持语言", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsDict cmsDict) {
        cmsDict.setDictType("language");
        cmsDict.setCode("language");
        return toAjax(cmsDictService.updateCmsDict(cmsDict));
    }

    /**
     * 删除分类
     */
    @PostMapping("/remove")
    @Log(title = "删除网站支持语言", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@RequestBody Long[] ids) {
        return toAjax(cmsDictService.deleteCmsDictByIds(ids));
    }


    @DeleteMapping("/{ids}")
    @Log(title = "删除网站支持语言", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cmsDictService.deleteCmsDictByIds(ids));
    }
}
